package com.example.service;

import com.example.mapper.CityMapper;
import com.example.mapper.PolicyMapper;
import com.example.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.security.Policy;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PolicyService {

    @Autowired
    private PolicyMapper policyMapper;
    @Autowired
    private CityMapper cityMapper;

    public List<PolicyVo> getList(ConditionsQuery conditionsQuery) {
        List<PolicyVo> list=policyMapper.getMedicalPolicyAndCityList(conditionsQuery);
        return list;
    }

    /**
     *selectCityId:三级联动最末尾的城市id
     * @param policyVo
     * @return
     */


    public Result addPolicy(Map<String, String> policyVo) {
        /**
         * 非空校验
         */
//        if (policyVo.get("id") == null || !StringUtils.hasText(policyVo.get("id"))) {
//            return new Result(100, "id不能为空", null);
//        }
        if (policyVo.get("title") == null || !StringUtils.hasText(policyVo.get("title"))) {
            return new Result(100, "缺少标题", null);
        }
        if (policyVo.get("message") == null || !StringUtils.hasText(policyVo.get("message"))) {
            return new Result(100, "缺少内容", null);
        }
        if (policyVo.get("selectCityId") == null || !StringUtils.hasText(policyVo.get("selectCityId"))) {
            return new Result(100, "缺少地区", null);
        }
        PolicyVo policy=new PolicyVo();
        policy.setTitle(policyVo.get("title"));
        policy.setMessage(policyVo.get("message"));
        //city_id 应该通过往city表里添加新数据获得city_id,selectCityId为city表中的city_number
        //policy.setCity_id(Integer.parseInt(policyVo.get("selectCityId")));

        //city表中先添加相应的城市
        CityVo cityVo=new CityVo();
        cityVo.setCity_number(Integer.parseInt(policyVo.get("selectCityId")));
        cityVo.setCreatetime(new Date());
        cityVo.setUpdatetime(new Date());
        try{
            int row=cityMapper.addCityByCityNumber(cityVo);
            if(row!=1){
                return new Result(100, "修改失败", null);
            }else{
                //medcial_policy表中添加
                CityVo cityVo1=cityMapper.getCityByCityNumber(cityVo.getCity_number());
                policy.setCity_id(cityVo1.getCity_id());
                policy.setCreate_time((new Date()));
                policy.setUpdate_time((new Date()));
                int row1=policyMapper.insert(policy);
                if(row1!=1){
                    return new Result(100, "修改失败", null);
                }

                // 如果保存成功，返回成功结果
                return new Result(200, "政策添加成功", null);
            }
        }catch (DataAccessException d){
            System.err.println(d);
            return new Result(100, "修改失败", null);
        }

    }


    public Result editPolicy(Map<String, String> newPolicy) {
        /**
         * 非空校验
         */
        if (newPolicy.get("title") == null || !StringUtils.hasText(newPolicy.get("title"))) {
            return new Result(100, "缺少标题", null);
        }
        if (newPolicy.get("message") == null || !StringUtils.hasText(newPolicy.get("message"))) {
            return new Result(100, "缺少内容", null);
        }
        PolicyVo policyVo=new PolicyVo();
        policyVo.setId(Integer.parseInt(newPolicy.get("id")));
        policyVo.setTitle(newPolicy.get("title"));
        policyVo.setMessage(newPolicy.get("message"));
        policyVo.setUpdate_time((new Date()));
        int row=policyMapper.updateById(policyVo);
        if(row!=1){
            return new Result(100, "修改失败", null);
        }

        // 如果保存成功，返回成功结果
        return new Result(200, "政策添加成功", null);
    }

    public Result delPolicyById(int id) {
        /**
         * 查询是否存在
         */
        PolicyVo p=policyMapper.getPolicyById(id);
        if(p==null){
            return new Result(100,"id不存在",null);
        }
        //先删除对应的city数据
        int row= cityMapper.delCityById(p.getCity_id());
        if(row!=1){
            return new Result(100,"操作失败",null);
        }

        row=policyMapper.delPolicyById(id);
        if (row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"操作成功",null);
    }

    public Result delBatch(String ids) {
        if (!StringUtils.hasText(ids)){
            return new Result(100,"请至少选择一条记录",null);
        }

        String[] idStr =ids.split(",");

        List<Integer> idList=new ArrayList<>();

        for (int i=0; i<idStr.length;i++){
            idList.add(Integer.parseInt(idStr[i]));
        }
        int rows =policyMapper.delBatch(idList);
        if(rows!=idList.size()){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"删除成功",null);
    }
}
